package com.yao.mall.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.mall.annotation.AuthCheck;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.IdRequest;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.GoodsSkuRequest;
import com.yao.mall.model.dto.goods.GoodsAddRequest;
import com.yao.mall.model.dto.goods.GoodsQueryRequest;
import com.yao.mall.model.dto.goods.GoodsRequest;
import com.yao.mall.model.dto.goods.HotGoodsRequest;
import com.yao.mall.model.vo.GoodsBasicVO;
import com.yao.mall.model.vo.GoodsVO;
import com.yao.mall.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jiahua.yao
 * @version 1.0
 * @description 商品管理控制层
 * @date 2024/3/7 16:54
 */
@RestController
@RequestMapping("/goods")
@Slf4j
@Tag(name = "商品信息相关接口")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @GetMapping("/get")
    @Operation(summary = "获得所有商品最基本信息（用于首页展示）")
    public Result<List<GoodsBasicVO>> getGoodsBasicAll() {
        List<GoodsBasicVO> goodsBasic = goodsService.getGoods();
        return Result.success(goodsBasic);
    }

    @PostMapping("/basic")
    @Operation(summary = "根据条件获取商品信息")
    public Result<Page<GoodsBasicVO>> getGoodsBasic(@RequestBody GoodsQueryRequest goodsQueryRequest) {
        Page<GoodsBasicVO> goodsBasic = goodsService.getGoodsBasic(goodsQueryRequest);
        return Result.success(goodsBasic);
    }

    @PostMapping("/add")
    @Operation(summary = "商品添加")
    @AuthCheck(mustRole = "1")
    public Result<Boolean> addGoods(@RequestBody GoodsAddRequest goodsAddRequest) {
        if(goodsAddRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"参数不可为空");
        }
        Boolean aBoolean = goodsService.addGoods(goodsAddRequest);
        return Result.success(aBoolean);
    }

    @PostMapping("/detail")
    @Operation(summary = "获取商品详情")
    public Result<GoodsVO> getGoodsDetails(@RequestBody IdRequest idRequest) {
        if(idRequest.getId() == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        GoodsVO goodsVO = goodsService.getGoodsDetails(idRequest);
        return Result.success(goodsVO);
    }

    @PostMapping("/hotGoods")
    @Operation(summary = "获取商品热榜")
    public Result<List<GoodsBasicVO>> getHotGoods(@RequestBody HotGoodsRequest hotGoodsRequest){
        if(hotGoodsRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        List<GoodsBasicVO> goodsBasicVOList = goodsService.getHotGoods(hotGoodsRequest);
        return Result.success(goodsBasicVOList);

    }

    @PostMapping("/changeStatus")
    @AuthCheck(mustRole = "1")
    //前端传spuId，spuStatus
    @Operation(summary = "商品上下架")
    public Result<Boolean> changeStatus(@RequestBody GoodsRequest goodsRequest) {
        Boolean aBoolean = goodsService.changeStatusById(goodsRequest);
        return Result.success(aBoolean);
    }

    @PostMapping("/changeStock")
    @AuthCheck(mustRole = "1")
    //前端传spuId，spuStatus
    @Operation(summary = "sku商品库存修改")
    public Result<Boolean> changeStock(@RequestBody GoodsSkuRequest goodsSkuRequest) {
        if(goodsSkuRequest==null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Boolean aBoolean = goodsService.changeStockById(goodsSkuRequest);
        return Result.success(aBoolean);
    }

    @DeleteMapping("{id}")
    @AuthCheck(mustRole = "1")
    //前端传spuId，spuStatus
    @Operation(summary = "商品删除")
    public Result<Boolean> deleteGoods(@PathVariable Long id) {
        Boolean aBoolean = goodsService.deleteGoods(id);
        return Result.success(aBoolean);
    }

}
